package com.teamsun.rptsql.oracle;

import com.teamsun.rptcfg.BaseRpt;
import com.teamsun.rptsql.AbstraceConcateSQL;
import com.teamsun.rptsql.IDBFunction;

import java.util.Map;

/**
 * <p>ORACLE数据库拼接SQL</p>
 */
public class OracleConcateSQL extends AbstraceConcateSQL {

    private IDBFunction oracleFunction;

    public OracleConcateSQL(IDBFunction dbFunction) {
        this.oracleFunction = dbFunction;
    }

    /**
     * ORACLE拼接分页查询SQL(CUBE数据)
     * @param baseRpt 报表配置信息
     * @param params 查询条件
     * @param startIndex 分页查询起始行
     * @param endIndex 分页查询结束行
     * @return
     */
    @Override
    public String concatePageSQL(BaseRpt baseRpt, Map<String, String> params, int startIndex, int endIndex) {
        String baseSelectSQL = concateSelectSQL(baseRpt, params, oracleFunction);
        String pageSelectSQL = "SELECT c.*,ROWNUM rn FROM (" + baseSelectSQL + ") c WHERE ROWNUM <= " + endIndex;
        pageSelectSQL = "SELECT d.*, ROWNUM rn FROM (" + pageSelectSQL + ") d WHERE rn >= " + startIndex;
        return pageSelectSQL;
    }

    /**
     * ORACLE拼接分页查询SQL(非CUBE数据)
     * @param baseRpt 报表配置信息
     * @param params 查询条件
     * @param startIndex 分页查询起始行
     * @param endIndex 分页查询结束行
     * @return
     */
    @Override
    public String concateCubePageSQL(BaseRpt baseRpt, Map<String, String> params, int startIndex, int endIndex) {
        String baseSelectSQL = concateCubeSelectSQL(baseRpt, params, oracleFunction);
        String pageSelectSQL = "SELECT c.*,ROWNUM rn FROM (" + baseSelectSQL + ") c WHERE ROWNUM <= " + endIndex;
        pageSelectSQL = "SELECT d.*, ROWNUM rn FROM (" + pageSelectSQL + ") d WHERE rn >= " + startIndex;
        return pageSelectSQL;
    }
}
